<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-11-26
 * Time: ����3:23
 */
class PrivilegeModel extends Model
{
    /**
     * 用户登录
     *
     * @param string $userName 用户名
     * @param string $pwd 密码
     * @return bool
     */
        public function login($userName,$pwd)
    {
        $users = D("user");
        $data['uname'] = $userName;
        $data['pwd'] = intval($pwd);
        $data['admin_level'] = 0;
        $user = $users->where($data)->select();
        if (!empty($user)) {
            return $user;
        } else {
            return false;
        }
    }

    /**
     * 获取用户权限
     *
     * @param $uid 用户ID
     * @return integer
     */
        public function get_level($uid){
        $data['uid'] = $uid;
        $user = D("user");
        $arr = $user->field("admin_level")->where($data)->find();
        if (!empty($arr)) {
            return $arr['admin_level'];
        } else {
            return null;
        }
    }

    /**
     * 查询用户列表
     * @param $level 用户权限、级别
     * @return array
     */
    public function query_user_list($level = 1)
    {
        $data['admin_level'] = $level;
        $arr = D("user")->where($data)->select();
        return $arr;
    }
    /**
     * 根据用户ID获取查询条件
     *
     * @param $uid  用户ID
     * @param array $data 用于封装条件的数组
     * @return array 返回数组
     */
    public function getArea($uid, array $data = array())
    {
        $uid = intval($uid);
        $temp = D('User')->where(array('uid' => $uid))->find();
        if ($temp) {
            array_push($data, $temp['area']);
            if ($temp['father_id'] > 1) {
                return $this->getArea($temp['father_id'], $data);
            }
        }
        return $data;
    }


    /**
     *获取用户列表
     * @param $level 用户权限级别
     * @return array
     */
    public function get_user_list($level){
        $level = intval($level);
        $users = D("user");
        if ($level == 1) {//市级用户
            $data['admin_level'] = 1;
            $arr = $users->where($data)->select();
            return $arr;
        } else if ($level == 2) {//区级用户
            $data['admin_level'] = 2;
            $arr = $users->where($data)->select();
            return $arr;
        } else if ($level == 3) {//园级用户
            $data['admin_level'] = 3;
            $arr = $users->where($data)->select();
            return $arr;
        } else {
            return null;
        }
    }

    //�޸��û���Ϣ
    //�޸�˼·�������û����ݹ������û�ID��ѯ���������ĸ�����
    //1.������м����޸����У���1���û�����2�����루3������
    //2.���������:�޸�����:(1)�û���(2)����(4)����
    //3.�����԰��:�޸�����:(1)�û���(2)����(3)԰��
    /**
     *更新用户信息
     *
     * @param $uid  用户ID
     * @param $userName 用户名
     * @param $pwd  密码
     * @param $area 区域
     * @return bool
     */
    public function update_user_info($uid,$userName,$pwd,$area){
        $user = D("user");
        $data['uid'] = $uid;
        $arr = $user->where($data)->select();
        if(!empty($arr)){//��ʾ���ڸ��û�
            $datatwo['uname'] = $userName;
            $datatwo['pwd'] = $pwd;
            $datatwo['area'] = $area;
            $bool = $user->where($data)->save($datatwo);
            if($bool){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    //ɾ���û���Ϣ
    public function delete_user_info($uid){
        $bool = $this->is_exists($uid);
        if($bool){//�������
            //ִ��ɾ������
            D("user")->where(array('uid'=>$uid))->delete();
            return true;
        }else{
            return false;
        }
    }
    //随机获取用户名
    public function get_random_userName($length=6,$mode=3){
        switch ($mode)
        {
            case '1':
                $str='123456789';
                break;
            case '2':
                $str='abcdefghijklmnopqrstuvwxyz';
                break;
            case '3':
                $str='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
                break;
            case '4':
                $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
                break;
            case '5':
                $str='ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
                break;
            case '6':
                $str='abcdefghijklmnopqrstuvwxyz1234567890';
                break;
            default:
                $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
                break;
        }
        $checkstr='';
        $len=strlen($str)-1;
        for ($i=0;$i<$length;$i++)
        {
            //$num=rand(0,$len);//产生一个0到$len之间的随机数
            $num=mt_rand(0,$len);//产生一个0到$len之间的随机数
            $checkstr.=$str[$num];
        }
        return $checkstr;
    }

    /**
     * 添加用户信息
     * @param $level 用户级别
     * @param $user 用户名
     * @param $pwd 密码
     * @param string $area 管理区域
     * @return bool  布尔值
     */
    public function add_user_info($level,$user,$pwd='123',$area,$type,$fid){
        $data['uname'] = $this->get_random_userName();
        $data['pwd'] = $pwd;
        $data['admin_level'] = $level;
        $data['area'] = $area;
        $data['type']=$type;
        $data['father_id'] = $fid;
        if($level == 1 || $level == 2 || $level == 3) {
            //查询该用户名是否存在
            $arr = D("user")->where(array('uname' => $user))->select();
            if (!empty($arr)) {
                return false;
            } else {
                $bool = D("user")->add($data);
                if ($bool) {
                    return true;
                } else {
                    return false;
                }
            }
        }
//        }else{//教师用户
//            $data['area'] = '';//
//            $data['admin_level'] = 5;//����ȼ�Ϊ5
//            $arr = D("users")->where(array('uname'=>$user))->select();
//            if(!empty($arr)){
//                return false;
//            }else{
//                $bool = D("users")->add($data);
//                if($bool){
//                    return true;
//                }else{
//                    return false;
//                }
//            }
//        }
    }
    //��֤�û���Ϣ�Ƿ����
    public function is_exists($uid){
        $arr = D("user")->where(array('uid'=>$uid))->select();
        if(!empty($arr)){
            return true;
        }else{
            return false;
        }
    }

    //ע:�м��˻�����ɸѡ����ǰҵ�����м��ͱ����˻�һ�������߶���˻����������ɸѡ
    /**
     * �����û�ɸѡ
     * �������û�ɸѡֱ��ͨ����������ɸѡ
     * @param $area
     * @return bool
     * �������е������Ϣ
     */
    public function special_search_user($area=''){
        $data['area'] = $area;
        $arr = D("user")->where($data)->select();
        if(!empty($arr)){
            return $arr;
        }else{
            return false;
        }
    }
    /**
     * ԰���û�ɸѡ
     * ˼·�� //�����ж����
       //1һ��һ�е����
       //(1)����Ϊ��,԰����
       //(2)԰��Ϊ��,������
       //2.���е����
       //3.��Ϊ�յ����
     * @param $area_name ����
     * @param $garden_name ԰��
     * @return array
     */
    public function ordinary_search_user($area_name='',$garden_name='',$level=3){
        $uids = $this->search_userId_by_area(trim($area_name));
        if((trim($area_name) == '' || trim($area_name == null))&&
            trim($garden_name)!=''&& trim($garden_name) !=null){//�жϵ�����Ϊ��,����$԰����Ϊ��
                $data['admin_level'] = $level;//�û�Ȩ��Ϊ3��
                //$data['father_id'] = array('in',$uids);����Ϊ�վͲ���������Ϊ��ѯ������
                $data['area'] = $garden_name; //��԰����Ϊ��ѯ������
                $arr = D("user")->where($data)->select();
                if(!empty($arr)){
                    return $arr;
                }else{
                    return array(
                        'status'=>0,
                        'message'=>'û���ҵ��������'
                    );
                }
        }else if((trim($area_name)!=''&& trim($area_name) != null) &&
            trim($garden_name) == '' || trim($garden_name) == null){//�жϵ�������Ϊ�գ�԰��Ϊ�յ������
            $data['admin_level'] = $level;
            $data['father_id'] = array('in',$uids);
            $arr = D("user")->where($data)->select();
            if(!empty($arr)){
                return $arr;
            }else{
                return array(
                    'status'=>0,
                    'message'=>'û���ҵ��������'
                );
            }
        }else if(trim($garden_name)!=''&& trim($garden_name) !=null &&
            trim($area_name)!=''&& trim($area_name) != null){//���е������
            $data['admin_level'] = $level;//�û�Ȩ��Ϊ3��
            $data['area'] = $garden_name; //��԰����Ϊ��ѯ������
            $data['father_id'] = array('in',$uids);
            $arr = D("user")->where($data)->select();
            if(!empty($arr)){
                return $arr;
            }else{
                return array(
                    'status'=>0,
                    'message'=>'û���ҵ��������'
                );
            }
        }

    }

    /**
     * ����������ѯ����Ӧ���û�ID
     * @param $area ����
     * @return array
     */
    public function search_userId_by_area($area){
       // $arr = array();
        $data['area'] = $area;
        $arr = D("user")->field('uid')->where($data)->select();
        $uids = array_column($arr,'uid');
        foreach($uids as $row){
            intval($row);
        }
        return $uids;
    }

    /*清除session*/
    public function logout(){
        //清除session
        session("username",null);
    }

    /*删除*/
    public function delete_user($uid){

    }

}













